/**
 * 定义接口
 * 使用express.Router来定义接口
 * 使用db.query访问数据库进行查询
 */
const express = require('express')
const router = express.Router()
const db = require('../database')
router.get('/getUserInfo', (req, res) => {
  let keyObj = req.query || {};
  let sql = 'SELECT * FROM userinfos'
  if (Object.values(keyObj).length != 0) {
    let key = Object.keys(keyObj)[0]
    sql = `${sql} where ${key}=${keyObj[key]}`
  }
  db.query(sql, (err, result) => {
    if (err) {
      res.send({
        status: 400,
        message: err
      })
    }
    if (result.length < 1) {
      res.send({
        status: 1,
        message: '查询失败'
      })
    }
    res.send({
      status: 0,
      message: '成功',
      data: result
    })

  })
})

router.post('/getUser', (req, res) => {
  let keyObj = req.body || {};
  let sql = 'SELECT * FROM userinfos'
  console.log(req.body, req)
  if (Object.values(keyObj).length != 0) {
    let key = Object.keys(keyObj)[0]
    sql = `${sql} where ${key}=${keyObj[key]}`
  }
  db.query(sql, (err, result) => {
    if (err) {
      res.send({
        status: 400,
        message: err
      })
    }
    if (result.length < 1) {
      res.send({
        status: 1,
        message: '查询失败'
      })
    }
    res.send({
      status: 0,
      message: '成功',
      data: result
    })

  })
})

module.exports = router